package com.android.volley.caches;

import android.util.LruCache;

import com.android.volley.images.ImageDataLoader;

public class LruImageDataCache implements ImageDataLoader.ImageCache{
    private static LruCache<String, byte[]> memoryCache;
    private static LruImageDataCache lruImageCache;

    private LruImageDataCache() {
        int maxMemory = (int) Runtime.getRuntime().maxMemory();
        int cacheSize = maxMemory / 8;
        memoryCache = new LruCache<String, byte[]>(cacheSize) {
            @Override
            protected int sizeOf(String key, byte[] value) {
                return value.length;
            }
        };
    }

    public static LruImageDataCache getInstance() {
        if (lruImageCache == null)
            lruImageCache = new LruImageDataCache();

        return lruImageCache;
    }

    public byte[] getData(String url) {
        return memoryCache.get(url);
    }

    public void setData(String url, byte[] data) {
        if (memoryCache.get(url) == null) {
            memoryCache.put(url, data);
        }
    }
}
